﻿Imports Microsoft.VisualBasic
Imports System.Data

Public Class BaseUserControl
    Inherits System.Web.UI.UserControl
    Public Sub objSetFocus(ByVal obj As System.Web.UI.WebControls.WebControl)
        RunClientScriptForAjax(Me.Page, "setTimeout(""$get('" & obj.ClientID & "').focus(); "", 10);")

    End Sub
    Protected Property dbName() As String
        Get
            If Session("dbName") = Nothing Then
                Return ""
            End If
            Return CType(Session("dbName"), String)
            'Return "AA"
        End Get
        Set(ByVal value As String)
            Session("dbName") = value
        End Set
    End Property

    ''' <summary>
    ''' 取得登入者姓名
    ''' </summary>
    ''' <value></value>
    ''' <returns>取得登入者姓名</returns>
    ''' <remarks>取得登入者姓名</remarks>
    Protected ReadOnly Property ActualUserName() As String
        Get
            If Session("ActualUserName") = Nothing Then
                Return ""
            End If

            Return CType(Session("ActualUserName"), String)
        End Get

    End Property

    Protected ReadOnly Property ActualUID() As String
        Get
            If Session("ActualUID") = Nothing Then
                Return ""
            End If
            Return CType(Session("ActualUID"), String)
        End Get
    End Property
#Region "runClientScript:從Server端寫出Client端執行的程式碼"
    Public Shared Sub RunClientScript(ByVal obj As Object, ByVal Script As String)
        Dim cMsgr As ClientScriptManager = CType(obj, Page).ClientScript
        Dim strKey As String = GetNewKey("RCS")
        cMsgr.RegisterStartupScript(obj.GetType, strKey, Script, True)
    End Sub

    Public Shared Sub RunClientScriptForAjax(ByVal obj As Page, ByVal Script As String)
        Dim strKey As String = GetNewKey("RSCA")
        ScriptManager.RegisterStartupScript(obj, obj.GetType(), strKey, Script, True)
    End Sub
#End Region

    ''' <summary>
    ''' 秀出訊息
    ''' </summary>
    ''' <param name="msg"></param>
    ''' <remarks>秀出訊息</remarks>
    Public Sub ShowMessage(ByVal msg As String)
        'Dim tmp As String = msg.Replace("'", "")
        'tmp = tmp.Replace(Chr(13), " "c)
        'tmp = tmp.Replace(Chr(10), " "c)
        'tmp = tmp.Replace(Chr(9), " "c)
        ''tmp = HttpUtility.HtmlEncode(tmp)
        'Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "ShowMessage", "<script language=JavaScript> alert( '" + tmp.Trim() + "');</script>")
        Dim script As String
        script = "<script language=JavaScript>  if (window.parent.frames[""framSplit""]!=null ) { window.parent.frames[""framSplit""].document.getElementById(""txtMessaage"").value ='" + msg.Replace("'", "").Replace("""", "\""").Replace(vbCrLf, "\n\r") + "'; } else { "
        script += "alert('" & msg.Replace("'", "").Replace("""", "\""").Replace(vbCrLf, "\n\r") & "'); }</script>"
        Page.ClientScript.RegisterClientScriptBlock(GetType(BasePage), "ShowMessage" + GetNewKey("RSCA"), script)

    End Sub

    ''' <summary>
    ''' 有UpdatePanel時秀出訊息
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="Msg"></param>
    ''' <remarks>有UpdatePanel時秀出訊息</remarks>
    Public Shared Sub ShowMessageForAjax(ByVal obj As Object, ByVal Msg As String)
        Dim script As String
        script = " if (window.parent.frames[""framSplit""]!=null ) { window.parent.frames[""framSplit""].document.getElementById(""txtMessaage"").value ='" + Msg.Replace("'", "").Replace("""", "\""").Replace(vbCrLf, "\n\r") + "'; } else { "
        script += "alert('" & Msg.Replace("'", "").Replace("""", "\""").Replace(vbCrLf, "\n\r") & "'); }"
        ' script += "alert('" & Msg.Replace("'", "").Replace("\", "\\").Replace("""", "\""").Replace(vbCrLf, "\n\r") & "'); "

        ScriptManager.RegisterStartupScript(obj, GetType(BasePage), "ShowMessage" + GetNewKey("RSCA"), script, True)


    End Sub

    ''' <summary>
    ''' 有UpdatePanel時開啟選取視窗,傳回值,如為ReadOnly或者Enabled=False則postBack後會恢復之前的狀態,需注意
    ''' </summary>
    ''' <param name="obj">傳入網頁上之任一webControl均可</param>
    ''' <param name="FieldsName">要秀出在視窗中的資料欄位</param>
    ''' <param name="ChnFieldNames">欄位的中文名稱</param>
    ''' <param name="tableName">資料表名稱</param>
    ''' <param name="dbName">連線字串的設定名稱</param>
    ''' <param name="returnFields">回傳欄位的順序號例1,3代表回第1及第3個欄位值中間以','分開</param>
    ''' <param name="returnControls">回傳的頁面webControl.ClientID,如傳回多個則中間以','分開</param>
    ''' <param name="feature">開啟視窗的外觀樣式</param>
    ''' <remarks> 有UpdatePanel時開啟選取視窗,傳回值,如為ReadOnly或者Enabled=False則postBack後會恢復之前的狀態,需注意</remarks>
    Public Sub PopupFormForAjax(ByVal obj As Object, ByVal FieldsName As String, ByVal ChnFieldNames As String, ByVal tableName As String _
                              , ByVal dbName As String, ByVal returnFields As String, ByVal returnControls As String, ByVal feature As String)

        Session("FieldsName") = FieldsName
        Session("ChnFieldNames") = ChnFieldNames
        Session("tableName") = tableName
        Session("dbName") = dbName
        Session("returnFields") = returnFields
        Session("returnControls") = returnControls

        Dim sb As New StringBuilder()
        Dim vbCrLf As String = "" & Chr(13) & ""
        sb.Append(vbCrLf)
        sb.Append("<script>" + vbCrLf)
        sb.Append("var w ; w=window.open('" & Me.ResolveUrl("~/Webform/PopForm/PopupForm.aspx") & "','aa','" & feature & "'); w.focus();")
        sb.Append("</script>" + vbCrLf)

        ScriptManager.RegisterStartupScript(obj, GetType(BasePage), "PopForm", sb.ToString(), False)


    End Sub


    Public Sub PopupFormForAjax(ByVal obj As Object, ByVal FieldsName As String, ByVal ChnFieldNames As String, ByVal dbTable As DataTable _
                             , ByVal dbName As String, ByVal returnFields As String, ByVal returnControls As String, ByVal feature As String)

        Session("FieldsName") = FieldsName
        Session("ChnFieldNames") = ChnFieldNames
        Session("tableName") = ""
        Session("dbName") = dbName
        Session("returnFields") = returnFields
        Session("returnControls") = returnControls
        Session("dbTable") = dbTable
        Dim sb As New StringBuilder()
        Dim vbCrLf As String = "" & Chr(13) & ""
        sb.Append(vbCrLf)
        sb.Append("<script>" + vbCrLf)
        sb.Append("var w ; w=window.open('" & Me.ResolveUrl("~/Webform/PopForm/PopupForm.aspx") & "','aa','" & feature & "'); w.focus();")
        sb.Append("</script>" + vbCrLf)

        ScriptManager.RegisterStartupScript(obj, GetType(BasePage), "PopForm", sb.ToString(), False)


    End Sub


    Public Sub PopupFormForAjax(ByVal obj As Object, ByVal FieldsName As String, ByVal ChnFieldNames As String, ByVal dbTable As DataTable _
                             , ByVal dbName As String, ByVal returnFields As String, ByVal returnControls As String, ByVal feature As String, ByVal PopMsg As String)

        Session("FieldsName") = FieldsName
        Session("ChnFieldNames") = ChnFieldNames
        Session("tableName") = ""
        Session("dbName") = dbName
        Session("returnFields") = returnFields
        Session("returnControls") = returnControls
        Session("dbTable") = dbTable
        Session("PopMsg") = PopMsg
        Dim sb As New StringBuilder()
        Dim vbCrLf As String = "" & Chr(13) & ""
        sb.Append(vbCrLf)
        sb.Append("<script>" + vbCrLf)
        sb.Append("var w ; w=window.open('" & Me.ResolveUrl("~/Webform/PopForm/PopupForm.aspx") & "','aa','" & feature & "'); w.focus();")
        sb.Append("</script>" + vbCrLf)

        ScriptManager.RegisterStartupScript(obj, GetType(BasePage), "PopForm", sb.ToString(), False)


    End Sub


#Region "GetNewKey : 取的唯一的名稱"
    ''' <summary>
    ''' 取的唯一的名稱
    ''' </summary>
    ''' <param name="Kind">Prefix name</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetNewKey(ByVal Kind As String) As String
        Randomize()
        Dim intRandomNumber As Integer = Rnd() * 100000
        'Return Kind & Now.ToString("yyyyMMddHHmmss") & Now.Millisecond.ToString("000") & intRandomNumber.ToString()
        Return Kind & New Guid().ToString()
    End Function


#End Region
End Class
